package com.hypo.aop;

import com.hypo.mapper.OperateLogMapper;
import com.hypo.pojo.OperateLog;
import com.hypo.until.CurrentHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;

@Aspect
@Slf4j
//@Component
public class OperationLogAspect_annotation {
    //@Autowired
    private OperateLogMapper operateLogMapper;

    @Around("@annotation(com.hypo.annotation.LogOperation)")
    public Object around(ProceedingJoinPoint pjp) throws Throwable { //目标方法对象信息记录
        long startTime = System.currentTimeMillis();
        Object proceed = pjp.proceed();
        long endTime = System.currentTimeMillis();
        long costTime = endTime - startTime;

        OperateLog operateLog = new OperateLog();
        operateLog.setOperateEmpId(currentEmpId());
        operateLog.setOperateTime(LocalDateTime.now());
        operateLog.setClassName(pjp.getTarget().getClass().getName());
        operateLog.setMethodName(pjp.getSignature().getName());
        operateLog.setMethodParams(Arrays.toString(pjp.getArgs()));
        operateLog.setReturnValue(proceed.toString());
        operateLog.setCostTime(costTime);
        operateLogMapper.insert(operateLog);
        return proceed;
    }

    public int currentEmpId() {
        //return CurrentHolder.getCurrentId();
        return 1;
    }
}
